/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.talleres.Bl.MaestroBlLocal;
import mx.its.talleres.modelo.Maestro;

/**
 *
 * @author Yessy
 */
@ManagedBean
@SessionScoped
public class MaestroBean {
    @EJB
    private MaestroBlLocal maestroBl;
    

    /**
     * Creates a new instance of MaestroBean
     */
     private List<Maestro> lista;
    private Maestro maestro;
    private String accion;
    
    public MaestroBean() {
    }

    public Maestro getMaestro() {
        return maestro;
    }

    public void setMaestro(Maestro maestro) {
        this.maestro = maestro;
    }

    public List<Maestro> getLista() {
        lista= maestroBl.getAll();
        return lista;
    }
    
     public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        maestroBl.registrar(maestro);
                }else{
        if(accion.equalsIgnoreCase("Editar")){
        maestroBl.modificar(maestro);
        }
        else{
        if(accion.equalsIgnoreCase("Eliminar"))
            maestroBl.eliminar(maestro);
        }
        }

        return "MaestroLista";
    }

    //Listeners
    public void prepararNuevoRegistro(ActionEvent evt) {
        accion="Nuevo";
        maestro = new Maestro();
    }
   public void prepararEditar(ActionEvent evt) {
       accion="Editar";
      System.out.println(evt.getComponent().getClientId());        
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        maestro=lista.get(index);
    } 
   public void prepararEliminar(ActionEvent evt){
   accion="Eliminar";
   System.out.println(evt.getComponent().getClientId());
    int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        maestro=lista.get(index);
   
   }
}
